package h0902;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Subsets {
    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 3};
        System.out.println(getSubsets(nums));
    }

    public static List<List<Integer>> getSubsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<Integer> temp = new LinkedList<>();
        Arrays.sort(nums);
        backTrack(nums, res, temp, 0);
        return res;
    }

    private static void backTrack(int[] nums, List<List<Integer>> res, LinkedList<Integer> temp, int start) {
        res.add(new LinkedList<>(temp));
        for (int j = start; j < nums.length; j++) {
            //如果有重复元素，去掉重复子集
            if (j > start && nums[j] == nums[j - 1]) continue;
            temp.addLast(nums[j]);
            backTrack(nums, res, temp, j + 1);
            temp.removeLast();
        }
    }
}
